<?php

/**
 * This is the model class for table "client".
 *
 * The followings are the available columns in table 'client':
 * @property integer $id
 * @property string $name
 * @property string $surname
 * @property string $patronymic
 * @property string $email
 * @property string $password
 * @property string $company
 * @property string $date_registration
 * @property string $last_activity
 * @property string $description
 * @property string $black_list
 * @property integer $city_id
 * @property integer $discount_id
 * @property integer $client_status_id
 *
 * The followings are the available model relations:
 * @property City $city
 * @property ClientStatus $clientStatus
 * @property Discount $discount
 * @property ClientAuto[] $clientAutos
 * @property ClientTelephone[] $clientTelephones
 */
class BaseClient extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'client';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, surname, email, password, date_registration, last_activity, city_id, discount_id, client_status_id', 'required'),
			array('city_id, discount_id, client_status_id', 'numerical', 'integerOnly'=>true),
			array('name, surname, patronymic, email', 'length', 'max'=>50),
			array('password', 'length', 'max'=>40),
			array('company', 'length', 'max'=>100),
			array('black_list', 'length', 'max'=>3),
			array('description', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, name, surname, patronymic, email, password, company, date_registration, last_activity, description, black_list, city_id, discount_id, client_status_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'city' => array(self::BELONGS_TO, 'City', 'city_id'),
			'clientStatus' => array(self::BELONGS_TO, 'ClientStatus', 'client_status_id'),
			'discount' => array(self::BELONGS_TO, 'Discount', 'discount_id'),
			'clientAutos' => array(self::HAS_MANY, 'ClientAuto', 'client_id'),
			'clientTelephones' => array(self::HAS_MANY, 'ClientTelephone', 'client_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'surname' => 'Surname',
			'patronymic' => 'Patronymic',
			'email' => 'Email',
			'password' => 'Password',
			'company' => 'Company',
			'date_registration' => 'Date Registration',
			'last_activity' => 'Last Activity',
			'description' => 'Description',
			'black_list' => 'Black List',
			'city_id' => 'City',
			'discount_id' => 'Discount',
			'client_status_id' => 'Client Status',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('surname',$this->surname,true);
		$criteria->compare('patronymic',$this->patronymic,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('company',$this->company,true);
		$criteria->compare('date_registration',$this->date_registration,true);
		$criteria->compare('last_activity',$this->last_activity,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('black_list',$this->black_list,true);
		$criteria->compare('city_id',$this->city_id);
		$criteria->compare('discount_id',$this->discount_id);
		$criteria->compare('client_status_id',$this->client_status_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return BaseClient the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
